#include <cstdio>

#define max(a,b) ((a>b)?(a):(b))

int n, m;
int a, b, c, d;
int S[380];
int DP[50][50][50][50];

int main() {
	scanf("%d%d", &n, &m);
	for (int i=0; i<n; i++) {
		scanf("%d", &S[i]);
	}
	int temp;
	for (int i=0; i<m; i++) {
		scanf("%d", &temp);
		switch (temp) {
			case 1:
				a++;
				break;
			case 2:
				b++;
				break;
			case 3:
				c++;
				break;
			case 4:
				d++;
				break;
		}
	}
	DP[0][0][0][0] = S[0];
	for (int aa=0; aa<=a; aa++)
		for (int bb=0; bb<=b; bb++)
			for (int cc=0; cc<=c; cc++)
				for (int dd=0; dd<=d; dd++) {
					int s = S[aa+bb*2+cc*3+dd*4];
					if (aa>0) DP[aa][bb][cc][dd] = max(DP[aa][bb][cc][dd], DP[aa-1][bb][cc][dd]+s);
					if (bb>0) DP[aa][bb][cc][dd] = max(DP[aa][bb][cc][dd], DP[aa][bb-1][cc][dd]+s);
					if (cc>0) DP[aa][bb][cc][dd] = max(DP[aa][bb][cc][dd], DP[aa][bb][cc-1][dd]+s);
					if (dd>0) DP[aa][bb][cc][dd] = max(DP[aa][bb][cc][dd], DP[aa][bb][cc][dd-1]+s);
				}
	printf("%d\n", DP[a][b][c][d]);
	return 0;
}
